//
//  NetDataEngine.m
//  LimitFree
//
//  Created by lijinghua on 16/3/1.
//  Copyright © 2016年 lijinghua. All rights reserved.
//

#import "NetDataEngine.h"
#import "AFNetworking.h"

@implementation NetDataEngine

+(instancetype)sharedInstance{
    static NetDataEngine  *s_intance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        s_intance = [[NetDataEngine alloc]init];
    });
    return s_intance;
}

//对输入的url进行编码
- (NSString*)urlEncode:(NSString*)urlInput{
    return [urlInput stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
}

- (void)GET:(NSString*)url success:(SuccessBlockType)successBlock falied:(FailedBlockType)failedBlock{
    //对输入的url进行编码
    NSString *urlEncoded = [self urlEncode:url];
    
    AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc]init];
    
    //增加一种类型，text/html类型
    NSSet *set = manager.responseSerializer.acceptableContentTypes;
    NSMutableSet *acceptSet = [NSMutableSet setWithSet:set];
    [acceptSet addObject:@"text/html"];
    manager.responseSerializer.acceptableContentTypes = acceptSet;
    
    [manager GET:urlEncoded parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (successBlock) {
            successBlock(responseObject);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (failedBlock) {
            failedBlock(error);
        }
    }];
}

//获取app 列表的请求接口
- (void)requestAppList:(NSString*)url success:(SuccessBlockType)successBlock falied:(FailedBlockType)failedBlock{
    [self GET:url success:successBlock falied:failedBlock];
}

- (void)requestAppDetail:(NSString*)url success:(SuccessBlockType)successBlock falied:(FailedBlockType)failedBlock
{
   [self GET:url success:successBlock falied:failedBlock];
}

- (void)requestNearbyApp:(SuccessBlockType)successBlock falied:(FailedBlockType)failedBlock
{
    //获取用户的当前地理位置信息，使用CoreLocation框架
    //longitude=116.344539&latitude=40.034346
    CGFloat longitude = 116.344539;
    CGFloat latitude  = 40.034346;
    NSString *url = [NSString stringWithFormat:kNearAppUrl,longitude,latitude];
    [self GET:url success:successBlock falied:failedBlock];
}

- (void)requestCategory:(SuccessBlockType)successBlock falied:(FailedBlockType)failedBlock
{
    [self GET:kCateUrl success:successBlock falied:failedBlock];
}

@end
